/**********************************************************************
 ** Copyright (C) 2009 Nokia Corporation.
 ** All rights reserved.
 **
 ** $LICENSE_TEXT$
 **********************************************************************/

#include "bluetoothmanager.h"
#include "btsingledeviceselector.h"
#include "btserialportclient.h"
#include "serialCommHandler/qserialport.h"

#include <QDebug>

static const QString defaultAddress("00:01:95:0a:1f:74");

BluetoothManager::BluetoothManager(QObject *parent) :
    QObject(parent)
{
    m_isTryingDefault = true;
    mp_ioDevice = NULL;

    mp_deviceSelector = new BtSingleDeviceSelector(this);
    mp_serialClient = new BtSerialPortClient(this);
    setupConnections();
}

BluetoothManager::~BluetoothManager()
{
    if (mp_ioDevice != NULL) {
        if (mp_ioDevice->isOpen())
            mp_ioDevice->close();
        delete mp_ioDevice;
    }
}

void BluetoothManager::setupConnections()
{
    QObject::connect(mp_deviceSelector, SIGNAL(deviceSelected(QString)), this, SLOT(
            setupSerialConnection(QString)));

    QObject::connect(mp_serialClient, SIGNAL(interfaceBounded(QIODevice*)), this, SLOT(setupIODevice(QIODevice*)));
    QObject::connect(mp_serialClient, SIGNAL(error()), this, SLOT(processError()));
    QObject::connect(mp_serialClient, SIGNAL(disconnected()), this, SLOT(processDisconnected()));

    QObject::connect(this, SIGNAL(deviceDefined(QString)), mp_serialClient, SLOT(setupInterface(
            QString)));
}

void BluetoothManager::setupSerialConnection(QString serialDevice)
{
    if (!serialDevice.isEmpty())
        emit deviceDefined(serialDevice);
    else
        emit disconnected();
}

void BluetoothManager::setupIODevice(QIODevice* device)
{
    mp_ioDevice = device;
    if (mp_ioDevice != NULL) {
        QObject::connect(mp_ioDevice, SIGNAL(readyRead()), this, SLOT(processNewData()));

        mp_ioDevice->open(QIODevice::ReadWrite);
        emit connected();
        return;
    }
    emit disconnected();
}

void BluetoothManager::connect()
{
    m_isTryingDefault = true;
    setupSerialConnection(defaultAddress);
}

void BluetoothManager::disconnect()
{
    mp_serialClient->disconnect();
}

void BluetoothManager::sendData(QString data)
{
    qDebug() << "send data: " << data;
    if (mp_ioDevice != NULL) {
        mp_ioDevice->write(data.toLatin1());
    }
}

void BluetoothManager::processNewData()
{
    if (mp_ioDevice != NULL) {
        qDebug() << "available bytes: " << mp_ioDevice->bytesAvailable();
        QByteArray data = mp_ioDevice->readAll();
        qDebug() << "manager data: " << data;
        emit dataReceived(data);
    }
}

void BluetoothManager::processError()
{
    if (m_isTryingDefault) {
        m_isTryingDefault = false;
        mp_deviceSelector->show();
        return;
    }
    emit disconnected();
}

void BluetoothManager::processDisconnected()
{
    qDebug() << "disconnected";
    if (mp_ioDevice != NULL) {
        mp_ioDevice->close();
    }
    emit disconnected();
}
